import { NextResponse } from 'next/server'
import { ProductCopy } from '@/types/Model'
import prisma from '@/lib/db'
import { verifyToken } from '@/lib/auth'

export async function PUT(req: Request): Promise<Response> {
  try {
    const token = req.headers.get('Authorization')?.split(' ')[1]
    const payload = token ? verifyToken(token) : null;
  
    if (!payload) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
    }


    const copies: ProductCopy[] = await req.json()

    // 批量更新文案的pxh
    const updatePromises = copies.map(async (copy) => {
      await prisma.productCopy.update({
        where: { id: copy.id },
        data: {
          status: copy.status,
          pxh: copy.pxh
        }
      })
    })

    await Promise.all(updatePromises)

    return NextResponse.json({ message: '文案顺序更新成功' }, { status: 200 })
  } catch (error) {
    console.error('Error updating copy order:', error)
    return NextResponse.json(
      { error: '文案顺序更新失败' },
      { status: 500 }
    )
  }
}
